package com.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@SuppressWarnings("all")
public class Utils {

	// 需要解析的抖音地址
	// #在抖音，记录美好生活##英语 #今天你学会了吗 所以，今天的重点是什么？@抖音文化站 http://v.douyin.com/2c1qAC/
	// 复制此链接，打开【抖音短视频】，直接观看视频！
	// 复制此链接，打开【抖音短视频】，直接观看视频！";
	public static void main(String[] args) {
		try {
			String inputText = "http://v.douyin.com/YK2WxQ/";
			HttpResponse<String> response = Unirest.post(urlProcess(inputText)).header("cache-control", "no-cache")
					.asString();
			String header = response.getHeaders().toString();
			int startIndexOf = header.lastIndexOf("[");
			int lastIndexOf = header.lastIndexOf("]");
			String decodeUrl = header.substring(startIndexOf + 1, lastIndexOf);
			if (StringUtils.isEmpty(decodeUrl)) {
				throw new RuntimeException("解析出错，请联系管理员");
			} else {
				log.info("解析成功：" + decodeUrl);
			}
		} catch (UnirestException e) {
			e.printStackTrace();
		}
	}

	public static String urlProcess(String url2) {
		try {
			String url = decodeHttpUrl(url2);
			Document doc = null;
			try {
				doc = Jsoup.connect(url).timeout(12138).get();
			} catch (Exception e) {
				e.printStackTrace();
			}
			Elements elem = doc.getElementsByTag("script");
			String url1 = elem.toString();
			int start = url1.indexOf("playAddr");
			url1 = url1.substring(start);
			int end = url1.indexOf("\",");
			String processedurl = url1.substring(11, end).replaceAll("playwm", "play");
			return processedurl;
		} catch (Exception e) {
			throw new RuntimeException("错误：" + e);
		}

	}

	public static String decodeHttpUrl(String url) {
		// 检测是否有中文，如果没有中文就是直接地址
		boolean containChinese = isContainChinese(url);
		if (containChinese) {
			int start = url.indexOf("http");
			int end = url.lastIndexOf("/");
			String decodeurl = url.substring(start, end);
			return decodeurl;
		} else
			return url;
	}

	public static boolean isContainChinese(String str) {
		Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
		Matcher m = p.matcher(str);
		if (m.find()) {
			return true;
		}
		return false;
	}

	
	
//	public static void main(String[] args) throws Exception {
//	
//	SimpleDateFormat sdf1= new SimpleDateFormat("yyyy/MM/dd");
//	
//	
//	List<Date> list1=new ArrayList<>();
//	list1.add(sdf1.parse("2019/3/3"));
//	list1.add(sdf1.parse("2019/3/2"));
//	list1.add(sdf1.parse("2019/3/1"));
//	
//	List<Date> list2= list1.stream().sorted().collect(Collectors.toList());
//	boolean flag = true;
//	for (int i = 0; i < list2.size(); i++) {
//			Date  a =list2.get(i);
//			
//			Date  b = list2.get(i);
//			Calendar ca1 = Calendar.getInstance(); 
//			Calendar ca2 = Calendar.getInstance(); 
//			ca1.setTime(a); 
//			ca2.setTime(b); 
//			long distanceMin =( ca1.getTimeInMillis()- ca2.getTimeInMillis())/(1000*60*60*24); 
//			if (distanceMin!=1) {
//                flag = false;
//                System.out.print("期数不连续，请选择连续期数");
//                break;
//            }
//	}
//	System.out.println(flag);
//	
//}
}
